package ops.view.forms.component;

import javax.swing.JComponent;

import ops.view.component.NumberField;

public class NumberInput extends FormInput {

	private NumberField field;
	
	public NumberInput(String label) {
		super(label, true);
		
		field = new NumberField();
	}
	
	public NumberInput(String label, int size) {
		this(label, size, true);
	}
	
	public NumberInput(String label, int size, boolean required) {
		super(label, required);
		
		field = new NumberField(size);
	}
	
	public NumberInput(String label, int size, boolean required, boolean readOnly) {
		this(label, size, required);
		
		field.setEditable(readOnly);
	}

	@Override
	public Object getInputValue() {
		if (field.getText() == null || field.getText().equals("")) {
			return (Double) 0D;
		}
		
		return (Double) Double.parseDouble(field.getText());
	}

	@Override
	public JComponent getInputComponent() {
		return field;
	}
	
	@Override
	public void setInputValue(Object value) {
		field.setText(value.toString());
	}
	
	@Override
	public void clearInput() {
		field.setText(null);
	}
}
